package com.chukun.netty.zerocopy;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

/**
 * @author chukun
 * 传统io客户端代码
 */
public class TraditionalIOClient {

    public static void main(String[] args) throws IOException {
        Socket socket = new Socket();
        socket.connect(new InetSocketAddress(8899));

        FileInputStream fileInputStream = new FileInputStream("G:\\linux-bigdata\\apache\\hadoop\\hadoop-2.7.2.tar.gz");
        OutputStream outputStream = socket.getOutputStream();
        byte[] buffer = new byte[4096];
        long writedCount =0L;
        long markWriteCount=0;
        long startTime = System.currentTimeMillis();
        while((markWriteCount=fileInputStream.read(buffer))!=-1){
            outputStream.write(buffer);
            writedCount+=markWriteCount;
        }
        System.out.println("write into "+writedCount +" 耗时: "+(System.currentTimeMillis()-startTime));
        fileInputStream.close();
        socket.close();
        outputStream.close();
    }
}
